﻿using FluentAssertions;
using Microsoft.Extensions.Configuration;
using RestSharp;
using System.Net;
using System.Threading.Tasks;

namespace SpecFlowDemo.Specs.Drivers
{
    public class HttpClientDriver
    {
        protected readonly RestClient _client;
        protected readonly IConfiguration _configuration;

        private HttpStatusCode _httpStatusCode;
        public string TestSSS { get; set; } = "10";

        public HttpClientDriver(IConfiguration configuration)
        {
            _client = new RestClient("http://localhost:5000");
            _configuration = configuration;
        }

        public async Task Get(string url, object includedProperties = null)
        {
            var request = new RestRequest(url);

            if (includedProperties != null)
            {
                request.AddObject(includedProperties);
            }

            var response = await _client.GetAsync(request);
            _httpStatusCode = response.StatusCode;
        }

        public async Task Delee(string url, object includedProperties = null)
        {
            var request = new RestRequest(url);

            if (includedProperties != null)
            {
                request.AddObject(includedProperties);
            }

            var response = await _client.GetAsync(request);
            _httpStatusCode = response.StatusCode;
        }

        public async Task Put(string url, object includedProperties = null)
        {
            var request = new RestRequest(url);

            if (includedProperties != null)
            {
                request.AddObject(includedProperties);
            }

            var response = await _client.PutAsync(request);
            _httpStatusCode = response.StatusCode;
        }

        public async Task Post(string url, object includedProperties = null)
        {
            var request = new RestRequest(url);

            if (includedProperties != null)
            {
                request.AddObject(includedProperties);
            }

            var response = await _client.PostAsync(request);
            _httpStatusCode = response.StatusCode;
        }

        public void CheckResponseStatusCode(int expectedStatusCode)
        {
            ((int)_httpStatusCode).Should().Be(expectedStatusCode);
        }

        public async Task Login()
        {
            var defaultToken = "123";//_configuration["Token"];
            _client.AddDefaultHeader("Authorization", $"Bearer {defaultToken}");
        }
    }
}
